#include "Window.h"
#include "Scene.h"

	LPCTSTR ClsName = L"Basic";
	LPCTSTR WndName = L"Simple";

//LRESULT CALLBACK WndProcedure(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK WndProcedure(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	LRESULT result = 0;
	switch (uMsg)
	{
	case WM_CREATE:
		{
			break;
		}
	case WM_PAINT:
		{
			result = DefWindowProc(hWnd, uMsg, wParam, lParam);
			MainWindow::getInstance()->refresh();
			break;
		}
	case WM_SYSCOMMAND:
		{
			if(SC_CLOSE == wParam)
			{
				if(IDYES == MessageBox(hWnd, TEXT("Really Quit?"), TEXT("Confirm"), MB_YESNOCANCEL))
				{
					//MainWindow::getInstance()->quit();
					PostQuitMessage(WM_QUIT);
					result = DefWindowProc(hWnd, uMsg, wParam, lParam);
				}
			}
			else
			{
				result = DefWindowProc(hWnd, uMsg, wParam, lParam);
			}
			break;
		}
	case WM_COMMAND:
		{
			//Menu handling stuff!!!!! -- ToDo!!!
		}
	case WM_DESTROY:
		{
			PostQuitMessage(WM_QUIT);
			//or -- PostQuitMessage(0); -- or -- PostMessage(hWnd, WM_SYSCOMMAND, SC_CLOSE, 0);
			break;
		}
	default:
		{
			return DefWindowProc(hWnd, uMsg, wParam, lParam);
			break;
		}
	}
	return result;
}

MainWindow * MainWindow::s_pInstance = NULL;

MainWindow * MainWindow::getInstance(void)
{
	return s_pInstance;
}

MainWindow *MainWindow::createInstance(HINSTANCE hInstance, int nCmdShow)
{
	if(NULL != s_pInstance)
	{
		deleteInstance();
	}
	s_pInstance = new MainWindow(hInstance, nCmdShow);
	return s_pInstance;
}

void MainWindow::deleteInstance(void)
{
	if(NULL != s_pInstance)
	{
		delete s_pInstance;
		s_pInstance = NULL;
	}
}

MainWindow::MainWindow(HINSTANCE hInstance, int nCmdShow)
{
	m_hInstance = hInstance;
	m_nCmdShow = nCmdShow;
	m_hWnd = NULL;
	m_hMenu = NULL;

	init();
}

MainWindow::~MainWindow(void)
{
	//obmitted client code close
}

//MainWindow * MainWindow::createInstance(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
void MainWindow::init(void)
{
	//MSG Msg;
	//HWND hWnd;
	//WNDCLASSEX WndClsEx;

	m_WndClsEx.cbSize = sizeof(WNDCLASSEX);
	m_WndClsEx.style = CS_HREDRAW | CS_VREDRAW;
	m_WndClsEx.lpfnWndProc = WndProcedure;
	m_WndClsEx.cbClsExtra = 0;
	m_WndClsEx.cbWndExtra = 0;
	m_WndClsEx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	m_WndClsEx.hCursor = LoadCursor(NULL, IDC_ARROW);
	m_WndClsEx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	m_WndClsEx.lpszMenuName = NULL;
	m_WndClsEx.lpszClassName = ClsName;
	m_WndClsEx.hInstance = m_hInstance;
	m_WndClsEx.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

	RegisterClassEx(&m_WndClsEx);

	m_hWnd = CreateWindow(ClsName, WndName, WS_OVERLAPPEDWINDOW, 50, 50, 800, 600, NULL,
		NULL, m_hInstance, NULL);

	//obmitted menu creation code

	Scene::createInstance(m_hWnd);

	//ShowWindow(hWnd, SW_SHOWNORMAL);
	ShowWindow(m_hWnd, m_nCmdShow);
	UpdateWindow(m_hWnd);

	//obmitted client stuff code

// ---------- removed ??? ------------------------------------
	//while (GetMessage(&Msg, NULL, 0, 0))
	//{
	//	TranslateMessage(&Msg);
	//	DispatchMessage(&Msg);
	//}
	//return 0;
// --------- end of removed ----------------------------------

}

void MainWindow::refresh(void)
{
	Scene::getInstance()->refresh();
}

void MainWindow::exec(void)
{
	MSG msg;
	while(GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
}

//	LPCTSTR ClsName = L"Basic";
//	LPCTSTR WndName = L"Simple";
//
//LRESULT CALLBACK WndProcedure(HWND hWnd, UNIT Msg, WPARAM wParam, LPARAM lParam);
//
////{
////	LRESULT result = 0;
////	switch (uMsg)
////	{
////	case WM_CREATE:
////		{
////			break;
////		}
////	case WM_PAINT:
////		{
////			result = DefWndProcedure(hWnd, uMsg, wParam, lParam);
////			Window::getInstance()->refresh();
////			break;
////		}
////	case WM_SYSCOMMAND:
////		{
////			if(SC_CLOSE == wParam)
////			{
////				if(IDYES == MessageBox(hWnd, "Really quit?", "Confirm", MB_YESNOCANCEL))
////				{
////					Window::getInstance()->quit();
////					result = DefWndProcedure(hWnd, uMsg, wParam, lParam);
////				}
////			}
////			else
////			{
////				result = DefWndProcedure(hWnd, uMsg, wParam, lParam);
////			}
////			break;
////		}
////	//obmitted
////	case WM_DESTROY:
////		{
////			PostQuitMessage(WM_QUIT);
////			break;
////		}
////	default:
////		{
////			result = DefWndProcedure(hWnd, uMsg, wParam, lParam);
////			break;
////		}
////	}
////	return result;
////}
//
//void MainWindow(HINSTANCE hInstance, int nCmdShow)
//{
//	m_hInstance = hInstance;
//	m_nCmdShow = nCmdShow;
//	m_hWnd = NULL;
//
//	MSG Msg;
//	HWND hWnd;
//	WNDCLASSEX m_WndClsEx;
//
//	m_WndClsEx.cbSize = sizeof(WNDCLASSEX);
//	m_WndClsEx.style = CS_HREDRAW | CS_VERDRAW;
//	m_WndClsEx.lpfnWndProc = WndProcedure;
//	m_WndClsEx.cbClsExtra = 0;
//	m_WndClsEx.cbWndExtra = 0;
//	m_WndClsEx.hIcon = LoadIcon(NULL, IDC_APLLICATION);
//	m_WndClsEx.hCursor = LoadCursor(NULL, IDC_ARROW);
//	m_WndClsEx.hbrBackground = (HBRUSH)GetStockObjects(WHITE_BRUSH);
//	m_WndClsEx.lpszMenuName = NULL;
//	m_WndClsEx.lpszClassName = ClsName;
//	m_WndClsEx.hInstance = hInstance;
//	m_WndClsEx.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
//
//	RegisterClassEx(&m_WndClsEx);
//
//	m_hWnd = CreateWindow(ClsName, WndName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL,
//		NULL, hInstance, NULL);
//
//	ShowWindow(m_hWnd, m_nCmdShow);
//	UpdateWindow(m_hWnd);
//}